/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode ListNode;

ListNode* removeNthFromEnd(ListNode* head, int n)
{
    int tmp = n - 1;
    ListNode* p1 = head, * p2 = head;
    ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
    phead->next = head;
    ListNode* pre = phead;
    while (tmp)
    {
        tmp--;
        p1 = p1->next;
    }

    while (p1->next)
    {
        pre = p2;
        p2 = p2->next;
        p1 = p1->next;
    }

    pre->next = p2->next;

    return phead->next;
}